package bp.ccfast;

import bp.ccfast.portal.WinDocModel;
import bp.ccfast.portal.WindowTemplate;
import bp.ccfast.portal.WindowTemplateAttr;
import bp.ccfast.portal.WindowTemplates;
import bp.ccfast.portal.windowext.HtmlVarDtl;
import bp.ccfast.portal.windowext.HtmlVarDtls;
import bp.ccfast.portal.windowext.SSODtls;
import bp.ccfast.rpt.Rpt3D;
import bp.da.*;
import bp.difference.handler.DirectoryPageBase;
import bp.en.TSEntityMyPK;
import bp.sys.*;
import bp.tools.Json;
import bp.web.WebUser;
import com.google.gson.JsonArray;
import net.sf.json.JSONArray;

import java.util.Hashtable;

/**
 页面功能实体

 */
public class DataV_Lowcode extends DirectoryPageBase
{
    /**
     页面功能实体
     */
    public DataV_Lowcode()
    {
    }

    public final String Init_LowcodePage() throws Exception {
        DataSet ds = new DataSet(); //定义容器.

        String pageID = this.GetRequestVal("PageID");
        String paraStr = this.GetRequestVal("Para");
        AtPara para = new AtPara(paraStr);

        Hashtable ht = para.getHisHT();

        WindowTemplates windows = new WindowTemplates();
        windows.Retrieve(WindowTemplateAttr.PageID, pageID, "Idx");

        for (WindowTemplate window : windows.ToJavaList())
        {
            if (window.getWinDocModel().equals(WinDocModel.Html))
            {
                continue;
            }
            if (window.getWinDocModel().equals(WinDocModel.SSO) == true)
            {
                SSODtls dtls = new SSODtls(); //获得明细.
                dtls.Retrieve("RefPK", window.getNo(), "Idx");
                //加入集合.
                DataTable mydt = dtls.ToDataTableField("SSODtls" + window.getNo());
                ds.Tables.add(mydt);
            }
            ///#region 变量文本
            if (window.getWinDocModel().equals(WinDocModel.HtmlVar) == true)
            {
                HtmlVarDtls dtls = new HtmlVarDtls(); //获得明细.
                dtls.Retrieve("RefPK", window.getNo(), "Idx");
                for (HtmlVarDtl dtl : dtls.ToJavaList())
                {
                    if (dtl.getExp0().toLowerCase().contains("select") == false)
                    {
                        continue;
                    }
                    if (DataType.IsNullOrEmpty(window.getWinDocModel()) == true)
                    {
                        continue;
                    }
                    String exp0 = Glo.DealExp(dtl.getExp0(),ht);
                    try{
                        DataTable dt = SFDBSrcs.RunSQLReturnTable(exp0, dtl.getDBSrc());
                        if(dt == null || dt.Rows.size() == 0)
                            dtl.setExp0("0");
                        String val = String.valueOf(dt.Rows.get(0).getValue(0));
                        dtl.setExp0(val);
                    }catch(Exception e){
                        dtl.setExp0("err@"+e.getMessage());
                    }

                }

                //加入集合.
                DataTable mydt = dtls.ToDataTableField("HtmlVarDtls" + window.getNo());
                ds.Tables.add(mydt);
            }
            ///#endregion 变量文本

            ///#region 折线图
            if (window.getWinDocModel().equals(WinDocModel.ChartLine) == true || window.getWinDocModel().equals(WinDocModel.ChartRing) == true
                    || window.getWinDocModel().equals(WinDocModel.ChartPie) == true || window.getWinDocModel().equals(WinDocModel.ChartLine) == true
                    || window.getWinDocModel().equals(WinDocModel.ChartZZT) == true || window.getWinDocModel().equals(WinDocModel.Table) == true
                    || window.getWinDocModel().equals(WinDocModel.ChartRose) == true || window.getWinDocModel().equals(WinDocModel.ChartLineAdv) == true
                    || window.getWinDocModel().equals(WinDocModel.ChartRadar) == true)
            {

                if (DataType.IsNullOrEmpty(window.getWinDocModel()) == true)
                {
                    continue;
                }
                try{
                    GenerDBSrc src = new GenerDBSrc();
                    src.setMyPK("Rpt." + pageID + "." + window.getNo() + ".Main");
                    //新数据源存储方式
                    if (src.RetrieveFromDBSources()> 0 )
                    {
                        DataTable dbsrcDT = GloGenerDBSrc.ExecGenerDBSrc_SFTableSearch(src, ht);
                        window.setDocs(bp.tools.Json.ToJson(dbsrcDT)) ;
                    }
//                    else
//                    {
                        //历史数据改造时的一种写法
//                        src.setMyPK("Node." + window.getNo() + ".Main");
//                        if (src.RetrieveFromDBSources()> 0 )
//                        {
//                            DataTable dbsrcDT = GloGenerDBSrc.ExecGenerDBSrc_SFTableSearch(src, null);
//                            window.setDocs(bp.tools.Json.ToJson(dbsrcDT)) ;
//                        }
//                        String sql = window.getDocs();
//                        sql = Glo.DealExp(sql, ht);
//                        window.setDocs(SFDBSrcs.RunSQLReturnTableJson(sql, window.getDBSrc()));
//                    }
                }catch(Exception e){
                    window.setDocs("err@"+e.getMessage());
                }

                String C0Ens = window.GetValStrByKey("C0Ens");
                if (DataType.IsNullOrEmpty(C0Ens) == false)
                {
                    try{
                        window.SetValByKey("C0Ens", SFDBSrcs.RunSQLReturnTableJson(C0Ens, window.getDBSrc()));
                    }catch(Exception e){
                        window.SetValByKey("C0Ens", "err@"+e.getMessage());
                    }
                }
            }
            if( window.getWinDocModel().equals(WinDocModel.ChartRate) == true){
                String SQLOfFZ = window.GetValStringByKey("SQLOfFZ");
                String SQLOfFM = window.GetValStringByKey("SQLOfFM");
                if(DataType.IsNullOrEmpty(SQLOfFZ) || DataType.IsNullOrEmpty(SQLOfFM) ){
                    continue;
                }
                SQLOfFZ = Glo.DealExp(SQLOfFZ, ht);//@hongyan.
                SQLOfFM = Glo.DealExp(SQLOfFM, ht);
                try{
                    int fzCount = DBAccess.RunSQLReturnValInt(SQLOfFZ.replace("~","'"));
                    int fmCount = DBAccess.RunSQLReturnValInt(SQLOfFM.replace("~","'"));
                    window.setDocs("[{\"Name\":\""+window.GetValStringByKey("LabOfFZ")+"\",\"Num\":"+fzCount+"},{\"Name\":\""+window.GetValStringByKey("LabOfFM")+"\",\"Num\":"+fmCount+"}]");
                }catch(Exception e){
                    window.setDocs("err@"+e.getMessage());
                }

            }
            ///#endregion 折线图

        }

        //增加到json.
        DataTable dt = windows.ToDataTableField("Windows");
        ds.Tables.add(dt);

        //json.
        return Json.ToJson(ds);
    }

    public final String Win_Dtl() throws Exception {
        WindowTemplate win = new WindowTemplate(this.getPKVal());
        String exp = win.GetValStringByKey("DtlExp");
        String key = this.GetRequestVal("Key");
        if(win.getWinDocModel() == WinDocModel.ChartRate && key.equals("FM"))
            exp = win.GetValStringByKey("DtlFMExp");
        exp = exp.replace("@Key",key);
        exp = bp.wf.Glo.DealExp(exp,null);
        DataTable dt = SFDBSrcs.RunSQLReturnTable(exp,win.getDBSrc());
        win.SetValByKey("DtlExp",bp.tools.Json.ToJson(dt));
        return bp.tools.Json.ToJson(win.ToDataTableField());
    }

    /// 根据跳转详情获取明细
    public String GetJumpDtl() throws Exception {

        String MyPK = this.GetRequestVal("MyPK");
        TSEntityMyPK en = new TSEntityMyPK("TS.CCFast.Windows.JumpDtl");
        en.setMyPK(MyPK);
        int rct = en.RetrieveFromDBSources();
        String sql = en.GetValByKey("DtlExp").toString();
        sql = bp.wf.Glo.DealExp(sql, null);
        DataTable prj = DBAccess.RunSQLReturnTable(sql);
        return bp.tools.Json.ToJson(prj);

    }

    /**
     * 三维
     * @return
     */
    public String Rpt3D_Init() throws Exception {
        Rpt3D rpt3D = new Rpt3D();
        rpt3D.setNo(this.GetRequestVal("RptID"));
        if(rpt3D.RetrieveFromDBSources() == 0)
            return "err@没有找到三维报表的信息";
        //获取三维的具体信息
        String doc = rpt3D.GetValStringByKey("Docs");
        String tag2 = rpt3D.GetValStringByKey("Tag2");
        String tag4 = rpt3D.GetValStringByKey("Tag4");
        String tag6 = rpt3D.GetValStringByKey("Tag6");
        String dbSrcNo = rpt3D.GetValStringByKey("DBSrc");
        if(DataType.IsNullOrEmpty(doc) || DataType.IsNullOrEmpty(tag2) || DataType.IsNullOrEmpty(tag4) || DataType.IsNullOrEmpty(tag6))
            return "err@三维的内容数据源、一维、二维、三维的数据不能为空";
        SFDBSrc sfdbSrc = new SFDBSrc();
        sfdbSrc.setNo(dbSrcNo);
        if(sfdbSrc.RetrieveFromDBSources() == 0)
            sfdbSrc.setNo("local");
        //判断一下集合是否为集合
        doc = bp.wf.Glo.DealExp(doc,null);
        try{
            JSONArray json = JSONArray.fromObject(doc);
            rpt3D.SetValByKey("Docs", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(doc);
            rpt3D.SetValByKey("Docs", bp.tools.Json.ToJson(dt));
        }
        tag2 = bp.wf.Glo.DealExp(tag2,null);
        try{
            JSONArray json = JSONArray.fromObject(tag2);
            rpt3D.SetValByKey("Tag2", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(tag2);
            rpt3D.SetValByKey("Tag2", bp.tools.Json.ToJson(dt));
        }
        tag4 = bp.wf.Glo.DealExp(tag4,null);
        try{
            JSONArray json = JSONArray.fromObject(tag4);
            rpt3D.SetValByKey("Tag4", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(tag4);
            rpt3D.SetValByKey("Tag4", bp.tools.Json.ToJson(dt));
        }
        tag6 = bp.wf.Glo.DealExp(tag6,null);
        try{
            JSONArray json = JSONArray.fromObject(tag6);
            rpt3D.SetValByKey("Tag6", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(tag6);
            rpt3D.SetValByKey("Tag6", bp.tools.Json.ToJson(dt));
        }
        return bp.tools.Json.ToJson(rpt3D.ToDataTableField());
    }

    /**
     * 二维
     * @return
     */
    public String Rpt2D_Init() throws Exception {
        Rpt3D rpt3D = new Rpt3D();
        rpt3D.setNo(this.GetRequestVal("RptID"));
        if(rpt3D.RetrieveFromDBSources() == 0)
            return "err@没有找到二维报表的信息";
        //获取三维的具体信息
        String doc = rpt3D.GetValStringByKey("Docs");
        String tag2 = rpt3D.GetValStringByKey("Tag2");
        String tag4 = rpt3D.GetValStringByKey("Tag4");
        String dbSrcNo = rpt3D.GetValStringByKey("DBSrc");
        if(DataType.IsNullOrEmpty(doc) || DataType.IsNullOrEmpty(tag2) || DataType.IsNullOrEmpty(tag4))
            return "err@二维的内容数据源、一维、二维的数据不能为空";
        SFDBSrc sfdbSrc = new SFDBSrc();
        sfdbSrc.setNo(dbSrcNo);
        if(sfdbSrc.RetrieveFromDBSources() == 0)
            sfdbSrc.setNo("local");
        //判断一下集合是否为集合
        doc = bp.wf.Glo.DealExp(doc,null);
        try{
            JSONArray json = JSONArray.fromObject(doc);
            rpt3D.SetValByKey("Docs", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(doc);
            rpt3D.SetValByKey("Docs", bp.tools.Json.ToJson(dt));
        }
        tag2 = bp.wf.Glo.DealExp(tag2,null);
        try{
            JSONArray json = JSONArray.fromObject(tag2);
            rpt3D.SetValByKey("Tag2", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(tag2);
            rpt3D.SetValByKey("Tag2", bp.tools.Json.ToJson(dt));
        }
        tag4 = bp.wf.Glo.DealExp(tag4,null);
        try{
            JSONArray json = JSONArray.fromObject(tag4);
            rpt3D.SetValByKey("Tag4", bp.tools.Json.ToJson(bp.tools.Json.ConvertToDataTable(json)));
        }catch(Exception e){
            DataTable dt = sfdbSrc.RunSQLReturnTable(tag4);
            rpt3D.SetValByKey("Tag4", bp.tools.Json.ToJson(dt));
        }

        return bp.tools.Json.ToJson(rpt3D.ToDataTableField());
    }
}
